package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;
import java.util.Map;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }


    @Override
    public List<?> selectList(String statementid, Object... params) throws Exception {
        Executor executor = new simpleExecutor();
        Map<String, MappedStatement> mappedStatements = configuration.getMappedStatement();
        MappedStatement mappedStatement = mappedStatements.get(statementid);
        List<Object> query = executor.query(configuration, mappedStatement, params);
        return (List<?>) query;
    }


    @Override
    public <T> T selectOne(String statementid, Object... params) throws Exception {
        Executor executor = new simpleExecutor();
        Map<String, MappedStatement> mappedStatements = configuration.getMappedStatement();
        MappedStatement mappedStatement = mappedStatements.get(statementid);
        List<Object> query = executor.query(configuration, mappedStatement, params);
        Object o = query.get(0);
        return (T) o;
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        ClassLoader classLoader = DefaultSqlSession.class.getClassLoader();
        Object proxyInstance = Proxy.newProxyInstance(classLoader, new Class<?>[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String name = method.getName();
                String claaName = method.getDeclaringClass().getName();
                String statementId = claaName + "." + name;
                // 准备参数2：params:args
                // 获取被调用方法的返回值类型
                Type genericReturnType = method.getGenericReturnType();
                Map<String, MappedStatement> mappedStatements = configuration.getMappedStatement();
                MappedStatement mappedStatement = mappedStatements.get(statementId);
                String sqlType = mappedStatement.getSqlType();
                switch (sqlType) {
                    case "insert":
                        insert(statementId, args);
                        break;
                    case "delete":
                        delete(statementId, args);
                        break;
                    case "update":
                        update(statementId, args);
                        break;
                    case "select":
                        if (genericReturnType instanceof ParameterizedType) {
                            List<Object> objects = (List<Object>) selectList(statementId, args);
                            return objects;
                        }
                        return selectOne(statementId, args);
                    default:
                        break;
                }
                return null;
            }
        });
        return (T) proxyInstance;
    }

    public void insert(String statementid, Object... params) throws Exception {
        Executor executor = new simpleExecutor();
        Map<String, MappedStatement> mappedStatements = configuration.getMappedStatement();
        MappedStatement mappedStatement = mappedStatements.get(statementid);
        executor.insert(configuration, mappedStatement, params);
    }

    public void update(String statementid, Object... params) throws Exception {
        Executor executor = new simpleExecutor();
        Map<String, MappedStatement> mappedStatements = configuration.getMappedStatement();
        MappedStatement mappedStatement = mappedStatements.get(statementid);
        executor.update(configuration, mappedStatement, params);
    }

    public void delete(String statementid, Object... params) throws Exception {
        Executor executor = new simpleExecutor();
        Map<String, MappedStatement> mappedStatements = configuration.getMappedStatement();
        MappedStatement mappedStatement = mappedStatements.get(statementid);
        executor.delete(configuration, mappedStatement, params);
    }
}
